package com.yuqing.sentimentapp.workspace.epidemic.material;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.linkface.baselibrary.http.BaseBean;
import com.yuqing.sentimentapp.R;
import com.yuqing.sentimentapp.base.BaseActivity;
import com.yuqing.sentimentapp.base.BaseObserver;
import com.yuqing.sentimentapp.module.ModuleManager;
import com.yuqing.sentimentapp.workspace.epidemic.material.entity.EpidemicMaterialInfoResult;
import com.yuqing.sentimentapp.workspace.epidemic.material.entity.EpidemicMaterialReportResult;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 防疫物资
 * Created by shenhe on 2020/4/3.
 */
public class EpidemicMaterialFillInActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.tv_save)
    TextView mTvSave;
    @BindView(R.id.et_pro_cloth_count)
    EditText mEtProClothCount;
    @BindView(R.id.et_mask_count)
    EditText mEtMaskCount;
    @BindView(R.id.et_goggles_count)
    EditText mEtGogglesCount;
    @BindView(R.id.et_glove_count)
    EditText mEtGloveCount;
    @BindView(R.id.et_disinfectant_count)
    EditText mEtDisinfectantCount;
    @BindView(R.id.et_alcohol_count)
    EditText mEtAlcoholCount;
    @BindView(R.id.et_reportor)
    EditText mEtReportor;
    @BindView(R.id.et_auditor)
    EditText mEtAuditor;
    @BindView(R.id.et_remark)
    EditText mEtRemark;


    private boolean editEnable = true;
    private EpidemicMaterialInfoResult.DataBean actionInfo;
    private EpidemicMaterialReportResult.DataBean.SubSumBean subSum;
    /**
     * 从已上报列表跳转过来的
     */
    private EpidemicMaterialReportResult.DataBean.ReportsBean reportsInfo;

    public static void goActivity(Activity activity, EpidemicMaterialInfoResult.DataBean actionInfo, EpidemicMaterialReportResult.DataBean.SubSumBean subSum) {
        Intent intent = new Intent(activity, EpidemicMaterialFillInActivity.class);
        intent.putExtra("actionInfo", actionInfo);
        intent.putExtra("subSum", subSum);
        activity.startActivity(intent);
    }

    public static void goActivity(Activity activity, EpidemicMaterialReportResult.DataBean.ReportsBean reportsInfo) {
        Intent intent = new Intent(activity, EpidemicMaterialFillInActivity.class);
        intent.putExtra("reportsInfo", reportsInfo);
        activity.startActivity(intent);
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_epidemic_material_fill_in;
    }

    @Override
    public void initPresenter() {
        Intent intent = getIntent();
        if (intent == null) return;
        actionInfo = ((EpidemicMaterialInfoResult.DataBean) intent.getSerializableExtra("actionInfo"));
        subSum = ((EpidemicMaterialReportResult.DataBean.SubSumBean) intent.getSerializableExtra("subSum"));

        reportsInfo = ((EpidemicMaterialReportResult.DataBean.ReportsBean) intent.getSerializableExtra("reportsInfo"));
    }

    @Override
    public void initView() {
        if (actionInfo != null) {
            mEtProClothCount.setText(actionInfo.getProClothCount());
            mEtAlcoholCount.setText(actionInfo.getAlcoholCount());
            mEtDisinfectantCount.setText(actionInfo.getDisinfectantCount());

            mEtGloveCount.setText(actionInfo.getGloveCount());
            mEtGogglesCount.setText(actionInfo.getGogglesCount());
            mEtMaskCount.setText(actionInfo.getMaskCount());

            mEtAuditor.setText(actionInfo.getAuditor());
            mEtReportor.setText(actionInfo.getReportor());
            mEtRemark.setText(actionInfo.getRemark());
        } else {
            if (subSum != null) {
                mEtProClothCount.setText(subSum.getProClothCount());
                mEtAlcoholCount.setText(subSum.getAlcoholCount());
                mEtDisinfectantCount.setText(subSum.getDisinfectantCount());

                mEtGloveCount.setText(subSum.getGloveCount());
                mEtGogglesCount.setText(subSum.getGogglesCount());
                mEtMaskCount.setText(subSum.getMaskCount());
            }
        }

        /**
         * 从已上报列表跳转过来的，不可编辑，隐藏保存按钮
         */
        if (reportsInfo != null) {
            mEtProClothCount.setText(reportsInfo.getProClothCount());
            mEtAlcoholCount.setText(reportsInfo.getAlcoholCount());
            mEtDisinfectantCount.setText(reportsInfo.getDisinfectantCount());

            mEtGloveCount.setText(reportsInfo.getGloveCount());
            mEtGogglesCount.setText(reportsInfo.getGogglesCount());
            mEtMaskCount.setText(reportsInfo.getMaskCount());

            mEtAuditor.setText(reportsInfo.getAuditor());
            mEtReportor.setText(reportsInfo.getReportor());
            mEtRemark.setText(reportsInfo.getRemark());

            mEtProClothCount.setEnabled(false);
            mEtAlcoholCount.setEnabled(false);
            mEtDisinfectantCount.setEnabled(false);

            mEtGloveCount.setEnabled(false);
            mEtGogglesCount.setEnabled(false);
            mEtMaskCount.setEnabled(false);

            mEtAuditor.setEnabled(false);
            mEtReportor.setEnabled(false);
            mEtRemark.setEnabled(false);

            mTvSave.setVisibility(View.GONE);
        }
}

    @OnClick({R.id.iv_back, R.id.tv_save})
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_save:
                if (TextUtils.isEmpty(mEtReportor.getText().toString())) {
                    showShortToast("填报人不可为空");
                    return;
                }
                if (TextUtils.isEmpty(mEtAuditor.getText().toString())) {
                    showShortToast("审核人不可为空");
                    return;
                }
                if (actionInfo == null) {
                    actionInfo = new EpidemicMaterialInfoResult.DataBean();
                    setData();
                    saveReportInfo(actionInfo);
                } else {
                    setData();
                    updateReportInfo(actionInfo);
                }
                break;
        }
    }

    private void setData() {
        actionInfo.setProClothCount(TextUtils.isEmpty(mEtProClothCount.getText().toString()) ? "0" : mEtProClothCount.getText().toString());
        actionInfo.setAlcoholCount(TextUtils.isEmpty(mEtAlcoholCount.getText().toString()) ? "0" : mEtAlcoholCount.getText().toString());
        actionInfo.setDisinfectantCount(TextUtils.isEmpty(mEtDisinfectantCount.getText().toString()) ? "0" : mEtDisinfectantCount.getText().toString());
        actionInfo.setGloveCount(TextUtils.isEmpty(mEtGloveCount.getText().toString()) ? "0" : mEtGloveCount.getText().toString());
        actionInfo.setGogglesCount(TextUtils.isEmpty(mEtGogglesCount.getText().toString()) ? "0" : mEtGogglesCount.getText().toString());
        actionInfo.setRemark(TextUtils.isEmpty(mEtRemark.getText().toString()) ? "" : mEtRemark.getText().toString());
        actionInfo.setMaskCount(TextUtils.isEmpty(mEtMaskCount.getText().toString()) ? "0" : mEtMaskCount.getText().toString());

        actionInfo.setAuditor(mEtAuditor.getText().toString());
        actionInfo.setReportor(mEtReportor.getText().toString());
    }

    private void updateReportInfo(EpidemicMaterialInfoResult.DataBean actionInfo) {
        ModuleManager.getInstance().updateEpidemicMaterialReportInfoResult(actionInfo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseBean>() {
                    @Override
                    protected void hideDialog() {
                        stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(BaseBean baseBean) {
                        showShortToast("保存成功");
                        finish();
                    }

                    @Override
                    protected void onFailure(String message) {
                        showShortToast(message);
                    }
                });
    }

    private void saveReportInfo(EpidemicMaterialInfoResult.DataBean actionInfo) {
        ModuleManager.getInstance().saveEpidemicMaterialReportInfoResult(actionInfo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseBean>() {
                    @Override
                    protected void hideDialog() {
                        stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(BaseBean baseBean) {
                        showShortToast("保存成功");
                        finish();
                    }

                    @Override
                    protected void onFailure(String message) {
                        showShortToast(message);
                    }
                });
    }
}
